package com.sky.service.impl;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.constants.Constants;
import com.sky.domain.*;
import com.sky.dto.OrderBackfeeDto;
import com.sky.dto.OrderBackfeeFormDto;
import com.sky.dto.OrderBackfeeItemDto;
import com.sky.mapper.CareOrderItemMapper;
import com.sky.mapper.OrderBackfeeItemMapper;
import com.sky.mapper.OrderBackfeeMapper;
import com.sky.mapper.OrderChargeItemMapper;
import com.sky.service.OrderBackfeeService;
import com.sky.vo.DataGridView;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * @author sky
 * @create 2021-07-19 16:14
 */
@Service
public class OrderBackfeeServiceImpl implements OrderBackfeeService {

    @Autowired
    private OrderBackfeeMapper orderBackfeeMapper;
    @Autowired
    private OrderBackfeeItemMapper orderBackfeeItemMapper;
    @Autowired
    private OrderChargeItemMapper orderChargeItemMapper;
    @Autowired
    private CareOrderItemMapper careOrderItemMapper;

    @Override
    public DataGridView queryAllOrderBackfeeForPage(OrderBackfeeDto orderBackfeeDto) {
        Page<OrderBackfee> page = new Page<>(orderBackfeeDto.getPageNum(), orderBackfeeDto.getPageSize());
        QueryWrapper<OrderBackfee> qw = new QueryWrapper<>();
        qw.like(StringUtils.isNotBlank(orderBackfeeDto.getPatientName()), OrderBackfee.COL_PATIENT_NAME, orderBackfeeDto.getPatientName());
        qw.like(StringUtils.isNotBlank(orderBackfeeDto.getRegId()), OrderBackfee.COL_REG_ID, orderBackfeeDto.getRegId());
        qw.orderByDesc(OrderBackfee.COL_CREATE_TIME);
        this.orderBackfeeMapper.selectPage(page, qw);
        return new DataGridView(page.getTotal(), page.getRecords());
    }

    @Override
    public List<OrderBackfeeItem> queryOrderBackfeeItemByBackId(String backId) {
        QueryWrapper<OrderBackfeeItem> qw = new QueryWrapper<>();
        qw.eq(null != backId, OrderBackfeeItem.COL_BACK_ID, backId);
        return this.orderBackfeeItemMapper.selectList(qw);
    }

    @Override
    public void saveOrderAndItems(OrderBackfeeFormDto orderBackfeeFormDto) {
        // 获取退费订单
        OrderBackfeeDto orderBackfeeDto = orderBackfeeFormDto.getOrderBackfeeDto();
        // 获取退费订单详情
        List<OrderBackfeeItemDto> orderBackfeeItemDtos = orderBackfeeFormDto.getOrderBackfeeItemDtos();

        // 添加退费订单
        OrderBackfee orderBackfee = new OrderBackfee();
        BeanUtils.copyProperties(orderBackfeeDto, orderBackfee);
        orderBackfee.setBackStatus(Constants.ORDER_STATUS_0);
        orderBackfee.setCreateTime(DateUtil.date());
        orderBackfee.setCreateBy(orderBackfeeFormDto.getSimpleUser().getUserName());
        this.orderBackfeeMapper.insert(orderBackfee);

        // 添加退费订单详情
        for (OrderBackfeeItemDto orderBackfeeItemDto : orderBackfeeItemDtos) {
            OrderBackfeeItem orderBackfeeItem = new OrderBackfeeItem();
            BeanUtils.copyProperties(orderBackfeeItemDto, orderBackfeeItem);
            // 订单关联退费订单ID
            orderBackfeeItem.setBackId(orderBackfeeDto.getBackId());
            orderBackfeeItem.setStatus(Constants.ORDER_DETAILS_STATUS_0);
            this.orderBackfeeItemMapper.insert(orderBackfeeItem);
        }
    }

    @Override
    public void backSuccess(String backId, String payPlatformId, String backType) {
        // 获取退费订单
        OrderBackfee orderBackfee = this.orderBackfeeMapper.selectById(backId);
        // 设置平台交易编号
        orderBackfee.setBackPlatformId(payPlatformId);
        // 设置支付类型
        orderBackfee.setBackType(backType);
        // 设置支付时间
        orderBackfee.setBackTime(DateUtil.date());
        // 修改订单状态
        orderBackfee.setBackStatus(Constants.ORDER_BACKFEE_STATUS_1); //已退费
        // 更新订单状态
        this.orderBackfeeMapper.updateById(orderBackfee);

        // 根据退费订单号查询退费详情
        QueryWrapper<OrderBackfeeItem> qw = new QueryWrapper<>();
        qw.eq(OrderBackfeeItem.COL_BACK_ID, backId);
        List<OrderBackfeeItem> orderBackfeeItems = this.orderBackfeeItemMapper.selectList(qw);
        // 获取各退费详情的Id
        List<String> allItemIds = new ArrayList<>();
        for (OrderBackfeeItem orderBackfeeItem : orderBackfeeItems) {
            allItemIds.add(orderBackfeeItem.getItemId());
        }

        // 更新退费单的详情状态
        OrderBackfeeItem orderBackItemObj = new OrderBackfeeItem();
        orderBackItemObj.setStatus(Constants.ORDER_DETAILS_STATUS_2); //已退费
        QueryWrapper<OrderBackfeeItem> orderBackItemQw = new QueryWrapper<>();
        orderBackItemQw.in(OrderBackfeeItem.COL_ITEM_ID, allItemIds);
        this.orderBackfeeItemMapper.update(orderBackItemObj, orderBackItemQw);

        // 更新收费详情的状态
        OrderChargeItem orderChargeItem = new OrderChargeItem();
        orderChargeItem.setStatus(Constants.ORDER_DETAILS_STATUS_2); //已退费
        QueryWrapper<OrderChargeItem> orderChargeItemQw = new QueryWrapper<>();
        orderChargeItemQw.in(OrderBackfeeItem.COL_ITEM_ID, allItemIds);
        this.orderChargeItemMapper.update(orderChargeItem, orderChargeItemQw);

        // 更新处方详情的状态
        CareOrderItem careOrderItem = new CareOrderItem();
        careOrderItem.setStatus(Constants.ORDER_DETAILS_STATUS_2);
        QueryWrapper<CareOrderItem> careOrderItemQw = new QueryWrapper<>();
        careOrderItemQw.in(CareOrderItem.COL_ITEM_ID, allItemIds);
        this.careOrderItemMapper.update(careOrderItem, careOrderItemQw);
    }
}
